function shake() {
	var that;
		this.speed = 30;
		this.getParams = new getParams();
}

shake.prototype = {
	params:'',
	number:0,
	shake_url:"../mobile/shaking",
	get_shake_url:"../mobile/getShake",
	open_id:'',
	meet_id:'',
	sequence:'',
	team:'',
	team_img:'',
	team_name:'',
	round:'',
	status:1,
    interval:1,
    timer:null,
    lastPower:0,
	init:function() {
		that = this;
		that.params = window.location.search;
		that.team = that.getParams.getParamsObj().team;
		that.round = that.getParams.getParamsObj().round;
		that.team_img = that.getParams.getParamsObj().team_img;
		that.team_name = that.getParams.getParamsObj().team_name;
		that.open_id = that.getParams.getQueryString("openid");
		that.meet_id = that.getParams.getQueryString("meet_id");
		that.sequence = that.getParams.getQueryString("sequence");
        that.interval = that.randoms(3);

		//	信息回填
		if(that.team =='blue')
		{
			$(".teamImg-group").addClass('blue');
		}
		$("#pageBody").find(".team-img").attr("src",that.team_img);
		$("#pageBody").find(".team-name").html(that.team_name);
		that.getParams.hideLoadMask();
		that.getShake();
	},
	//获取摇一摇次数
	getShake:function() {
		var data = {
			open_id:that.open_id,
			meet_id:that.meet_id,
			sequence:that.sequence,
			round:that.round,
		};
		that._request_data(that.get_shake_url,data,that._getShakeCallBack,that._getShakeErrorCallBack);
	},
	_getShakeCallBack:function(ret) {
		var num = parseInt(ret.num);
		if( num < 0 ) {
			console.log("error message");
		}else {
			that.lastPower = num;
			$("#power-num").html(num);
		}
		that.rocking();
	},
	_getShakeErrorCallBack:function(errorMsg) {
	},

	//摇一摇
	rocking:function() {
		var elem = $("#power-num");
		var i = 0;
        var flag = that.getParams.getQueryString("flag");
        if(flag) {
        	for (var i = 1; ; i++) {
    		 	if(that.status != 2){
	                that.number = i;
                    that._countNum();
	                elem.html(i);
                }
        	}
        	return;
        }
        if(window.DeviceMotionEvent) {
	        var x = y = z = lastX = lastY = lastZ = 0;
	        window.addEventListener('devicemotion', function(){
	            var acceleration =event.accelerationIncludingGravity;
	            var txt = 0;
	            x = acceleration.x;
	            y = acceleration.y;
	            z = acceleration.z;  
	            if(Math.abs(x-lastX) > that.speed || Math.abs(y-lastY) > that.speed  || Math.abs(z-lastZ) > that.speed) {
	                i += parseInt(1);
	                if(that.status != 2) {
		                that.number = i;
                        that._countNum();
		                elem.html(i + that.lastPower);
	                }
	            }
	            lastX = x;
	            lastY = y;
	            lastZ = z;
	        }, false);
	    }else {
	    	alert("对不起，你的设备无不支持摇一摇功能。")
	    }
	},
	_countNum:function() {
		var team = that.team.substr(1);
		var round = that.round;
		var data = {
				open_id:that.open_id,
				sequence:that.sequence,
				meet_id:that.meet_id,
				round : that.round,
				num:parseInt(that.number) + parseInt(that.lastPower)
			};
		that.timer = setTimeout(function() {
			that._request_data(that.shake_url,data,that._countNumCallBack,that._countNumErrorCallBack);
		},that.interval);
	},
	_countNumCallBack:function(ret) {
		that.status = parseInt(ret.res);
		if(that.status == 2 ) {
			window.location.href = "/tug/mobile/transition" + that.params;
		} else if(that.status < 0){
            if (that.status == -300) {
				that.getParams.showTips(ret.msg);
                window.location.href = '/tug/mobile/chooseside' +that.params;
                return false;
            } else {
                alert(ret.msg);
			}
		}
	},
	_countNumErrorCallBack:function(ret) {

	},
	_request_data:function(url,data,callback,errorCallback) {
		$.ajax({
			type:"post",
			url:url,
			dataType:"json",
			data:data,
			success:function(ret) {
				callback && callback(ret);
			},
			error:function() {
				errorCallback && errorCallback();
			}
		})
	},
	randoms:function(i) {
		return Math.ceil(Math.random()*i);
	}
    
}

$(function() {
	var obj = new shake();
		obj.init();
});